﻿

local moduleName = "Highlight"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Highlight",
	["Color"] = true,
	["Change the highlight color."] = true
}
if GetLocale() == "zhCN" then
	L[moduleName] = "高亮"
	L["Color"] = "颜色"
	L["Change the highlight color."] = "更改高亮效果的颜色"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "高亮"
	L["Color"] = ""
	L["Change the highlight color."] = ""
end

----------------------------------
--      Local Declaration      --
----------------------------------

local target = "target"

----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName, "AceHook-3.0")

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		color = {
			type = "color",
			name = L["Color"],
			desc = L["Change the highlight color."],
			get = function() return unpack(plugin.db.profile.color) end,
			set = function(_, r, g, b)
					plugin.db.profile.color[1] = r
					plugin.db.profile.color[2] = g
					plugin.db.profile.color[3] = b
					plugin:UpdateAllWith(function(frame)
						frame.parts[moduleName].texture:SetVertexColor(unpack(plugin.db.profile.color))
					end)
				end
		}
	}
}

plugin.defaultDB = {
	profile = {
		color = {0.7, 0.7, 0.3},
		style = {
			strata = 3
		}
	}
}
plugin.head = {
	core = true
}

----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	self:RegisterEvent("PLAYER_TARGET_CHANGED")
end


function plugin:OnCreate(frame)
	local highlight = CreateFrame("Frame", nil, frame)
	highlight:SetFrameLevel(21)
	highlight:ClearAllPoints()
	highlight:SetAllPoints(frame)
	highlight:Hide()

	local texture = highlight:CreateTexture(nil, "OVERLAY")
	texture:SetTexture("Interface\\Buttons\\UI-Listbox-Highlight2")
	texture:SetVertexColor(unpack(self.db.profile.color))
	texture:SetBlendMode("ADD")
	texture:SetAlpha(1)
	texture:ClearAllPoints()
	texture:SetAllPoints(highlight)
	texture:Show()
	
	highlight.texture = texture

	return highlight

end


function plugin:OnInactive(frame)
	if self:IsHooked(frame, "OnEnter") then
		self:Unhook(frame, "OnEnter")
		self:Unhook(frame, "OnLeave")
	end
end


function plugin:OnUpdate(frame)
	if not self:IsHooked(frame, "OnEnter") then
		self:HookScript(frame, "OnEnter")
		self:HookScript(frame, "OnLeave")
	end
end


function plugin:PLAYER_TARGET_CHANGED()
	if UnitIsEnemy("player", "target") then
		target = "targettarget"
	else
		target = "target"
	end

	self:UpdateAllWith(function(frame)
		if UnitIsUnit(frame.unit, target) then
			frame.parts[moduleName]:Show()
		else
			frame.parts[moduleName]:Hide()
		end
	end)
end


function plugin:OnEnter(frame)
	frame.parts[moduleName]:Show()
	if frame:GetAlpha() < 1.0 then
		frame:SetAlpha(0.3)
	end
	self.hooks[frame].OnEnter(frame)
end


function plugin:OnLeave(frame)
	if not UnitIsUnit(frame.unit, target) then
		frame.parts[moduleName]:Hide()
	end

	if frame:GetAlpha() < 1.0 then
		frame:SetAlpha(0.3)
	end
	self.hooks[frame].OnLeave(frame)
end
